<?php

namespace app\common\cachemodel\chat;

use app\common\model\BaseCache;
use XznsTools\cache\CacheManage;

class HzQuestion extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.hz_question';

    protected $cacheModel = 'explain.hz_question';

    /**
     * 获取所有的问题信息
     * @return array|bool|mixed|string
     * @throws \Exception
     */
    public function getAllQuestionList()
    {
        $ids = CacheManage::getInstance()->setCacheConfig($this->cacheModel)->setSort('DESC')->ZsGetIocData('list', function ()
        {
            try
            {
                // 状态开启  并且问题需要档案
                $data = $this->where(['status' => 1, 'need_recode' => 1])->order('id asc')->column('id');

                $resData = [];
                foreach ($data as $id)
                {
                    $resData[$id] = $id;
                }
                $data = $resData;
            }
            catch (\Exception $e)
            {
                $data = [];
            }
            return $data;
        }, true);
        return $this->batchGetInfoByIds(array_keys($ids));
    }

    /**
     * @param array $ids
     * @param array|null $type
     * @param string $sortType
     * @return array
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        try
        {
            $data = $this
                ->where(['id' => ['in', $ids]])
                ->column('id,status,need_recode,question,question_type,relation_type,ouput_type', 'id');

        }
        catch (\Exception $e)
        {
            $data = [];
        }

        return $data;
    }

    public function batchGetInfoByIds($cids, $field = [])
    {
        $resData = parent::batchGetInfoByIds($cids, $field);

        return $this->dataInit($resData);
    }

    /**
     * 数据初始化
     * @param array $data
     * @return array
     */
    public function dataInit(array $data)
    {
        foreach ($data as $index => $itemData)
        {
            $data[$index] = arrayKeyData2int($itemData, ['id', 'status', 'question_type', 'need_recode', 'relation_type', 'ouput_type']);
        }

        return $data;
    }

}